package com.letv.core.contentprovider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

/**
 * 用户信息内容提供者
 * 
 * @author zhuqiao
 *
 */
public class UserInfoContentProvider extends ContentProvider {
    public static final String AUTHORITY = "com.letv.android.client.provider.userinfoprovider";
    public static final int CODE_USERINFO = 1000;// 匹配码
    public static final Uri URI_USERINFO = Uri.parse("content://" + AUTHORITY + "/" + UserInfoDb.TABLE_NAME);

    private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);

    static {
        matcher.addURI(AUTHORITY, UserInfoDb.TABLE_NAME, CODE_USERINFO);
    }

    private UserInfoDb userInfoDb;

    @Override
    public boolean onCreate() {
        userInfoDb = new UserInfoDb(getContext());
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        Cursor cursor = null;
        int match = matcher.match(uri);
        SQLiteDatabase db = userInfoDb.getWritableDatabase();
        switch (match) {
            case CODE_USERINFO:
                cursor = db.query(UserInfoDb.TABLE_NAME, null, selection, selectionArgs, null, null, sortOrder);
                cursor.setNotificationUri(getContext().getContentResolver(), uri);
                break;
            default:
        }
        return cursor;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        long rowId;
        Uri newUri = null;
        int match = matcher.match(uri);
        SQLiteDatabase db = userInfoDb.getWritableDatabase();
        switch (match) {
            case CODE_USERINFO:
                rowId = db.insert(UserInfoDb.TABLE_NAME, null, values);
                if (rowId > 0) {
                    newUri = ContentUris.withAppendedId(URI_USERINFO, rowId);
                }
                break;
        }

        if (newUri != null) {
            getContext().getContentResolver().notifyChange(uri, null);
        }

        return newUri;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int count;
        int match = matcher.match(uri);
        SQLiteDatabase db = userInfoDb.getWritableDatabase();

        switch (match) {
            case CODE_USERINFO:
                count = db.delete(UserInfoDb.TABLE_NAME, selection, selectionArgs);
                break;
            default:
                throw new UnsupportedOperationException("Unknown or unsupported URL: " + uri.toString());
        }

        this.getContext().getContentResolver().notifyChange(uri, null);

        return count;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        int count;
        int match = matcher.match(uri);
        SQLiteDatabase db = userInfoDb.getWritableDatabase();

        switch (match) {
            case CODE_USERINFO:
                count = db.update(UserInfoDb.TABLE_NAME, values, selection, selectionArgs);
                break;
            default:
                throw new UnsupportedOperationException("Unknown or unsupported URL: " + uri.toString());
        }

        this.getContext().getContentResolver().notifyChange(uri, null);

        return count;
    }

}
